var PlayerController = require("PlayerController");

var WaveInfo = cc.Class({
    name: 'WaveInfo',
    properties: {
        enemyToSpawn: cc.Prefab,
        waveLength: 10,
        timeBetweenSpawns: 1,
    },
});

var EnemySpawner = cc.Class({
    extends: cc.Component,

    properties: {
        enemies: [cc.Prefab],
        spawnedEnemies: [cc.Node],
        //spawnedEnemiesPool: cc.NodePool,

        spawnTime: 0,
        spawnCounter: 0,
        despawnDistance: 0,

        checkEnemiesPerframe: 0,
        enemiesToCheck: 0,

        waves: [WaveInfo],
        currentWave: 0,
        waveCounter: 0,
    },

    statics: {
        instance: null,
    },
    ctor () {
        EnemySpawner.instance = this;
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

		// 屏幕的斜对角长度的一半
        this.despawnDistance = cc.v2(this.node.position).sub(cc.v2(cc.winSize.width, cc.winSize.height)).mag();

        this.currentWave = -1;
        this.goToNextWave();
    },

    update (dt) {

        if (PlayerController.instance.playerNode.active) {
            if (this.currentWave < this.waves.length) {
                this.waveCounter -= dt;

                if (this.waveCounter <= 0) {
                    this.goToNextWave();
                }

                this.spawnCounter -= dt;
                if (this.spawnCounter <= 0) {
                    this.spawnCounter = this.waves[this.currentWave].timeBetweenSpawns;
                    this.spawnEnemy(this.waves[this.currentWave].enemyToSpawn);
                }
            }
        }

        let checkTarget = this.enemiesToCheck + this.checkEnemiesPerframe;

        while (this.enemiesToCheck < checkTarget) {
            if (this.enemiesToCheck < this.spawnedEnemies.length) {
                if (this.spawnedEnemies[this.enemiesToCheck].active) {
					if (cc.v2(PlayerController.instance.playerNode.position).sub(this.spawnedEnemies[this.enemiesToCheck].position).mag() > this.despawnDistance) {
                        this.spawnedEnemies[this.enemiesToCheck].destroy();

                        this.spawnedEnemies.splice(this.enemiesToCheck, 1);
                        checkTarget--;
                    } else {
                        this.enemiesToCheck++;
                    }
                } else {
                    this.spawnedEnemies.splice(this.enemiesToCheck, 1);
                    checkTarget--;
                }
            } else {
                this.enemiesToCheck = 0;
                checkTarget = 0;
            }
        }
    },

    spawnEnemy (enemyToSpawn) {
        let enemy = cc.instantiate(enemyToSpawn);
        enemy.setPosition(this.spwanPosition());
        this.spawnedEnemies.push(enemy);
        this.node.addChild(enemy);
        enemy.zIndex = 1;
    },



    // 敌人随机在屏幕四周生成
    spwanPosition () {
        let position = cc.v2(0,0);

        let spawnVerticalEdge = Math.random() > 0.5 ? true : false;
        let spawnHorizontalEdge = Math.random() > 0.5 ? true : false;
        if (spawnVerticalEdge) {
			position.y = PlayerController.instance.playerNode.position.y -cc.winSize.height / 2 + Math.random() * cc.winSize.height;
            if (spawnHorizontalEdge) {
				position.x = PlayerController.instance.playerNode.position.x + cc.winSize.width / 2;
            } else {
				position.x = PlayerController.instance.playerNode.position.x - cc.winSize.width / 2;
            }
        } else {
			position.x = PlayerController.instance.playerNode.position.x - cc.winSize.width / 2 + Math.random() * cc.winSize.width;
            if (spawnHorizontalEdge) {
				position.y = PlayerController.instance.playerNode.position.y + cc.winSize.height / 2;
            } else {
				position.y = PlayerController.instance.playerNode.position.y - cc.winSize.height / 2;
            }
        }

        return position;
    },

	// 进入下一波
    goToNextWave (){
        this.currentWave++;
        if (this.currentWave >= this.waves.length) {
            this.currentWave = this.waves.length % this.waves.length;
        }

        this.waveCounter = this.waves[this.currentWave].waveLength;
        this.spawnCounter = this.waves[this.currentWave].timeBetweenSpawns;
    },
});

module.exports = EnemySpawner;
